package com.wdl.modules.myhouse.utils.util;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author 昏昏的君
 */
@Component
@Slf4j
public class RedisUtil {


    @Autowired
    private  StringRedisTemplate template;

    private static final Random random=new Random();

    /**
     * 随机过期时间
     * @param
     * @return
     */
    public static Integer getTimeOut(Integer min,Integer max){
        return random.nextInt(max)+min;
    }



    public  String getValue(String key){
      return template.opsForValue().get(key);
    }

    public  void setValue(String key, String value, long timeUnit,TimeUnit unit){
       template.opsForValue().set(key,value,timeUnit,unit);
    }
    public void delVal(String key){
        try {
            template.delete(key);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("redis删除key失败，key:"+key);
        }
    }


}
